package com.zl.zlibrary.utils;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.Toast;

/**
 * Created by zhanglei on 2017\12\8 0008.
 * 系统功能工具类
 */

public class SystemUtils {

    /**
     * 调用拨号界面
     *
     * @param phone 电话号码
     */
    public static void call(Context context, String phone) {

        if (!isPermission(Manifest.permission.CALL_PHONE, context)) {
            Toast.makeText(context, "没有拨打电话的权限，无法拨打电话", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(context, "无法拨打电话，手机号未被获取", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 检查是否有该权限
     *
     * @param permission
     * @return
     */
    private static boolean isPermission(String permission, Context context) {
        return (ContextCompat.checkSelfPermission(context, permission)
                == PackageManager.PERMISSION_GRANTED);
    }

    /**
     * 调用发短信界面
     *
     * @param context
     * @param phone
     */
    public static void sendSms(Context context, String phone) {
        Uri uri2 = Uri.parse("smsto:" + phone);
        Intent intentFinalMessage = new Intent(Intent.ACTION_VIEW, uri2);
//        intentFinalMessage.setType("vnd.android-dir/mms-sms");
        context.startActivity(intentFinalMessage);
    }

}
